const {
  MemberModel
} = require('../../../utils/member-model.js');
var memberModel = new MemberModel();
const {
  GiftShopModel
} = require('../../../utils/giftshop-model.js')
const giftShopModel = new GiftShopModel()
var utils =
  require("../../../utils/util.js");
const {
  globalData
} = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLogin: false,
    isIphoneX: globalData.isIphoneX,
    src: 'https://oss.kiscloud.net/wechat/images/no_phone.png',
    lottery: 0, //飞豆
    integral: 0, //积分
    memberName: "", //会员名称
    memberImg: "", //会员头像
    leverCode: "", //会员等级code
    leverName: "", //等级名称
    memberId: "", //会员ID
    svipType: "", //SVIP类型 1:是 0:否
    hasPwd: null, //0未设置密码，1已设置密码
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      isLogin: globalData.isLogin
    }, function () {
      this.getUserInfo(); //会员信息
      this.getUserAssetSummary(); //会员资产汇总
    })
  },

  /**
   * 会员资产汇总
   */
  getUserAssetSummary: function () {
    const shopInfo = wx.getStorageSync('shopInfo')
    if (globalData.isLogin) {
      giftShopModel.userAssetShop({
        merchantId: wx.getStorageSync('merchantId'), //商户ID
        shopId: shopInfo.shopId
      }).then(data => {
        var that = this;
        that.setData({
          lottery: utils.formatCurrency(data.lotteryTotal),
          integral: utils.formatCurrency(data.integralTotal),
        });
      }).catch(err => {
        wx.showToast({
          title: `${err.error}`,
          icon: 'none',
          duration: 2000
        })
      })
    }
  },

  /**
   *微信会员信息
   */
  getUserInfo: function () {
    if (globalData.isLogin) {
      memberModel.userInfo({
        merchantId: wx.getStorageSync('merchantId')
      }).then(data => {
        wx.setStorageSync('memberId', data.memberId);
        var that = this;
        that.setData({
          memberName: data.nickname || "", //会员名称
          src: data.avatarUrl || "https://oss.kiscloud.net/wechat/images/no_phone.png", //会员头像
          leverCode: data.memberLevel ? 'V' + data.memberLevel : "V0", //会员等级
          leverName: data.memberLevelName ? data.memberLevelName : "", //等级名称
          memberId: data.memberId ? data.memberId : "", //会员ID
          svipType: data.svipType, //SVIP类型 1: 是 0: 否
          hasPwd: data.hasPwd, //0未设置密码，1已设置密码
        });
      }).catch(err => {
        wx.showToast({
          title: `${err.error}`,
          icon: 'none',
          duration: 2000
        })
      })
    }
  },

  //跳转到个人资料
  goInformation: function () {
    if (globalData.isLogin) {
      wx.reportAnalytics('member_nickname')
      wx.navigateTo({
        url: '/pages/memberCenter/information/personalData/index'
      })
    } else {
      wx.navigateTo({
        url: '/pages/login/index'
      })
    }
  },

  //跳转到飞豆
  goLottery: function () {
    wx.reportAnalytics('gift_member_lottery');
    this.judgeIsLogin('/pages/memberCenter/lottery/index')
  },

  //跳转到积分
  goIntegral: function () {
    wx.reportAnalytics('gift_member_integral');
    this.judgeIsLogin('/pages/memberCenter/integral/index')
  },

  //跳转到我的订单
  goOrderList: function () {
    wx.reportAnalytics('gift_member_order');
    this.judgeIsLogin('/pages/giftShop/myOrder/orderList/index')
  },

  //跳转到换货/售后
  goAfterSale: function () {
    wx.reportAnalytics('gift_member_service');
    this.judgeIsLogin('/pages/giftShop/serviceOrder/serviceList/index')
  },

  //跳转到支付密码
  goPaymentCode: function () {
    wx.reportAnalytics('gift_member_password');
    this.judgeIsLogin('/pages/giftShop/password/phoneVerify/index?pwdType=' + this.data.hasPwd)
  },

  //跳转到地址管理
  goAddressList: function () {
    wx.reportAnalytics('gift_member_address');
    this.judgeIsLogin('/pages/giftShop/address/addressList/index')
  },

  // 判断是否授权登录
  judgeIsLogin(url) {
    if (globalData.isLogin) {
      wx.navigateTo({
        url
      })
    }
  }
})